<template>
<div style="width: 70%;margin-top: 5%">
  <el-form :model="resetPwdForm" status-icon :rules="resetPwdRules" ref="pwdForm" label-width="100px">
      <el-form-item label="用户名" prop="username">
      <!-- <el-input prefix-icon="iconfont icon-showpassword" placeholder="请输入用户名" v-model="resetPwdForm.username" autocomplete="off">
       </el-input> -->
       <span> {{showUsername}}</span>
    </el-form-item>

    <el-form-item label="原密码" prop="password">
      <el-input prefix-icon="iconfont icon-showpassword" placeholder="请输入原始密码" type="password" v-model="resetPwdForm.password" autocomplete="off" show-password>
       </el-input>
    </el-form-item>
    <el-form-item label="新密码" prop="new_password">
      <el-input prefix-icon="iconfont icon-showpassword" placeholder="请输入新密码" 
              type="password" v-model="resetPwdForm.new_password" autocomplete="off" show-password>
      </el-input>
    </el-form-item>
    <el-form-item label="确认密码" prop="password_again">
       <el-input prefix-icon="iconfont icon-showpassword" placeholder="请再次输入新密码" 
             type="password" v-model="resetPwdForm.password_again" autocomplete="off" show-password>
      </el-input>
    </el-form-item>
    <el-form-item>
       <el-button type="primary" @click="submitForm('pwdForm')">确认修改</el-button>
                    <el-button @click="resetForm('pwdForm')">重置</el-button>
    </el-form-item>
  </el-form>
</div>
</template>

<script>
import { check,update } from '@/api/changepwd'
    export default {
    name: "Changepwd",
    data() {
        var validatePass = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请输入密码'));
            } else {
                if (this.resetPwdForm.password_again !== '') {
                    this.$refs.pwdForm.validateField('password_again');
                }
                callback();
            }
        };
        var validatePass2 = (rule, value, callback) => {
            if (value === '') {
                callback(new Error('请再次输入密码'));
            } else if (value !== this.resetPwdForm.new_password) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
    return {
        ResetPwdDialogVisible: false,
            resetPwdForm: {
                username: '',
                password: '',
                new_password: '',
                password_again: '',
            },
            resetPwdRules: {
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' },
                    {
                        min: 6,
                        max: 18,
                        message: '用户密码的长度在6～18个字符',
                        trigger: 'blur'
                    },
                    { validator: validatePass, trigger: 'blur' }
                ],
                new_password: [
                    { required: true, message: '请输入新密码', trigger: 'blur' },
                    {
                        min: 6,
                        max: 18,
                        message: '用户密码的长度在6～18个字符',
                        trigger: 'blur'
                    },
                    { validator: validatePass, trigger: 'blur' }
                ],
                password_again: [
                    { required: true, message: '请确认新密码', trigger: 'blur' },
                    {
                        min: 6,
                        max: 18,
                        message: '用户密码的长度在6～18个字符',
                        trigger: 'blur'
                    },
                    { validator: validatePass2, trigger: 'blur' }
                ],
            }
        }
    },
    computed:{
    showUsername(){
      return Cookies.get('username')
    }
  },
    methods: {
    //   async checkUser(){
    //         var data = { 
    //         username:Cookies.get('username'), 
    //         password:Cookies.get('password')
    //         } 
    //       await this.$http.post("http://localhost:9090/user/check",data)
    //       .then(res =>{
    //           console.log(res)
    //           if(res.data.code ==200){
    //               this.$message.success('密码验证成功')
    //           }
    //           else{
    //               this.$message('密码错误')
    //           }
    //       })
    //       .catch()
    //   },
    submitForm(formName) {
    // this.resetPwdForm.username =  this.$store.getters.username;
    console.log(this.resetPwdForm);
    // console.log(this.$store.state);
    this.$refs[formName].validate(async valid => {
        if (!valid) return;
        // 表单验证成功后再发请求
        // var data = { 
        //     username:Cookies.get(username),
        //     password:this.resetPwdForm.password
        //     } 
        // const {status: status} = await this.$http.post("http://localhost:9090/user/check", data);
        const {status: status} = await check(Cookies.get(username),this.resetPwdForm.password);
        // if (status !== 200) return this.$message.error('重置密码失败！');
        if(status==200){
            this.$message.success('密码验证成功')
            // data = { 
            //     username:this.resetPwdForm.username, 
            //     password:this.resetPwdForm.new_password
            // }
            const {status: status} = await update(this.resetPwdForm.username,this.resetPwdForm.new_password);
            if (status !== 200) return this.$message.error('重置密码失败！');
            this.$message.success("重置密码成功，正在跳转登录页面");
            this.$router.push("/login").catch(err => {err})
        }
        else{
            this.$message('密码错误')
        }
    });
    },
    // 重置
    resetForm(pwdForm) { 
    this.$refs[pwdForm].resetFields();
        }
    },
    }
</script>

<style scoped>

</style>

